﻿//
// TravModel public API for file and directory operations
// Any updating of the GUI is handled asynchronously by the file system event handlers.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;

using FileTreeSpace;


namespace TravModelSpace
{
    public partial class TravModel
    {
        public bool RenameFile (string fullPathName, string oldBaseName, string newBaseName)
        {
            string failure = null;
            try
            {
                FileInfo file = new FileInfo (fullPathName + '\\' + oldBaseName);
                file.MoveTo (fullPathName + '\\' + newBaseName);
            }
            catch (IOException ex)
            {
                failure = "Cannot rename " + oldBaseName + ": " + ex.Message;
            }
            catch (System.UnauthorizedAccessException ex)
            {
                failure = oldBaseName + "\rCannot rename: " + ex.Message;
            }

            if (failure != null)
                Presenter.ShowMessage (failure, "Error Renaming");

            return failure != null;
        }


        public void DeleteSelectedFiles ()
        {
            string prompt;
            string failure = null;

            if (Detail.Presenter.SelectedCount == 0)
                return;

            int i = 0;
            string[] fileNames = new string[Detail.Presenter.SelectedCount];

            foreach (string fullName in Detail.Presenter.SelectedFullNames)
                fileNames[i++] = fullName;

            if (i == 1)
                prompt = "Delete '" + fileNames[0] + "'?";
            else
                prompt = "Delete " + i + " files?";

            bool okay = Presenter.ShowQuestion (prompt, "Confirmation Required");
            if (!okay)
                return;

            foreach (string fileName in fileNames)
            {
                try
                {
                    Debug.WriteLine ("Nuking file [" + fileName + "]");
                    // Nuke file!
                    FileInfo file = new FileInfo (fileName);
                    file.Delete ();
                }
                catch (IOException ex)
                {
                    failure = ex.Message;
                }
                catch (UnauthorizedAccessException ex)
                {
                    failure = ex.Message;
                }
            }

            if (failure != null)
                Presenter.ShowMessage (failure, "Delete Failed");
            else
                if (fileNames.Length == 1)
                    ShowStatus ("Deleted file " + fileNames[0]);
                else
                    ShowStatus ("Deleted " + i + " files");
        }


        public bool CreateDirectory (string parentPathName, string newLeafName)
        {
            string failure = null;
            try
            {
                String fullName = parentPathName + '\\' + newLeafName;

                if (Directory.Exists (fullName))
                    throw new IOException ("Cannot create " + newLeafName + @"\ because a directory with the same name already exists.");

                /*DirectoryInfo result =*/
                Directory.CreateDirectory (fullName);
            }
            catch (IOException ex)
            {
                failure = ex.Message;
            }

            if (failure != null)
            {
                Presenter.ShowMessage (failure, "Error Creating Folder");
            }

            return failure != null;
        }


        public bool RenameDirectory (string parentPathName, string oldLeafName, string newLeafName)
        {
            bool error = true;
            try
            {
                Debug.WriteLine ("Renaming file [" + oldLeafName + "] to [" + newLeafName + "]");
                DirectoryInfo dInfo = new DirectoryInfo (parentPathName + @"\" + oldLeafName);
                dInfo.MoveTo (parentPathName + @"\" + newLeafName);

                error = false;
            }
            catch (IOException ex)
            {
                Presenter.ShowMessage ("Cannot rename file: " + ex.Message, null);
            }

            return error;
        }


        public bool DeleteDirectory (string fullPathName)
        {
            string failure = null;
            try
            {
                Debug.WriteLine ("Nuking dir [" + fullPathName + "]");
                DirectoryInfo dInfo = new DirectoryInfo (fullPathName);

                // Nuke directory!
                dInfo.Delete (true);
            }
            catch (IOException ex)
            {
                failure = "Cannot delete file: " + ex.Message;
            }

            if (failure != null)
                Presenter.ShowMessage (failure, "Error Deleting");

            return failure != null;
        }
    }
}
